package com.chookss.home.search.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.Token;
import com.chookss.home.search.entity.SearchAllEntity;
import com.chookss.tiku.util.TiKuUtils;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.Nullable;

public class SearchNewsAdapter extends BaseQuickAdapter<SearchAllEntity.NewsBean.ListBean, BaseViewHolderEx> {
    private Context context;
    private int width, height;
    private String key;

    public SearchNewsAdapter(int layoutResId, Context context, @Nullable List<SearchAllEntity.NewsBean.ListBean> data) {
        super(layoutResId, data);
        this.context = context;

    }

    @Override
    protected void convert(BaseViewHolderEx helper, SearchAllEntity.NewsBean.ListBean item) {
        TextView tvTiltle = helper.getView(R.id.tvSearchNewsTitle);
        TextView tvSearchType = helper.getView(R.id.tvSearchNewsContains);
        TextView tvSearchProvider = helper.getView(R.id.tvSearchNewsProvider);
        ImageView ivSearch = helper.getView(R.id.ivSearchNews);
        if (Utils.isNull(item.getPicturePath())) {
            ivSearch.setVisibility(View.GONE);
        } else {
            ivSearch.setVisibility(View.VISIBLE);
            GlideUtils.loadCustomRound(context, item.getPicturePath(), ivSearch, R.drawable.default_news, 4);

        }
        tvTiltle.setText(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getNewsTitle(), key));
        tvSearchType.setText(TiKuUtils.subjectTypeName(item.getNewsContent()));

        if (!Utils.isNull(item.getEmployeeName())) {
            tvSearchProvider.setText("发布人：");
            tvSearchProvider.append(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getEmployeeName(), key));
        }
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String addressType = "";
                if (Urls.baseUrl.contains("dev_kmer")) {
                    addressType = "1";
                } else {
                    addressType = "0";
                }
                String url = "";
                if(!Utils.isNull(item.getQuoteCode())&&"1".equals(item.getQuoteFlag())){
                    url = Urls.tuwenDetail+"mainCode=" + item.getQuoteCode()+"&newsCode=" + item.getNewsCode() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                }else if("2".equals(item.getQuoteFlag())){
                    url =item.getNewsContent();
                }else{
                    url = Urls.webBaseUrl+"indexNew.html#/newDetail?newsCode=" + item.getNewsCode() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                }
                 Intent intent = new Intent(context, WebCommonActivity.class);
                intent.putExtra("url", url);
                intent.putExtra("isShare", true);
                intent.putExtra("newsTitle", item.getNewsTitle());
                intent.putExtra("createEmployeeName", item.getEmployeeName());
                intent.putExtra("resourceId", item.getNewsCode());
                intent.putExtra("newsContent", item.getNewsContent());
                intent.putExtra("picUrl", "");
                intent.putExtra("waterMark", "13");
                context.startActivity(intent);
                if (!Utils.isNull(item.getId())) {
                    toClickNum(item.getId());
                }
            }
        });
    }

    public void setKey(String key) {
        this.key = key;
    }

    private void toClickNum(String newsId) {
        Map<String, String> map = new HashMap<>();
        map.put("id", newsId);
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.setClickNum, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
}
